package com.kfm.wiki.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.kfm.wiki.entity.Category;
import com.kfm.wiki.entity.Category;
import com.kfm.wiki.service.CategoryService;
import com.kfm.wiki.mapper.CategoryMapper;
import com.kfm.wiki.util.PageReq;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
* @author 10854
* @description 针对表【category】的数据库操作Service实现
* @createDate 2022-10-17 14:34:27
*/
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
    implements CategoryService{

    @Override
    public Page<Category> page(Category category, PageReq page) {
        if (ObjectUtil.isEmpty(category)){
            return null;
        }
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        if (StringUtils.hasLength(category.getName())){
            wrapper.like("name", category.getName());
        }
        wrapper.orderByAsc("sort");
        PageHelper.startPage(page.getPage(), page.getSize());
        Page<Category> categorys = (Page<Category>) getBaseMapper().selectList(wrapper);
        return categorys;
    }

    @Override
    public List<Category> list(Category category) {
        if (ObjectUtil.isEmpty(category)){
            return null;
        }
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        if (StringUtils.hasLength(category.getName())){
            wrapper.like("name", category.getName());
        }
        wrapper.orderByAsc("sort");
        return getBaseMapper().selectList(wrapper);
    }

    @Override
    public boolean save(Category category) {
        if (ObjectUtil.isEmpty(category.getId())){
            category.setId(IdUtil.getSnowflakeNextId());
            return getBaseMapper().insert(category) == 1;
        } else {
            return getBaseMapper().updateById(category) == 1;
        }
    }
}




